package com.haibei.activity;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.common.net.NetResult;
import com.common.task.BaseTask;
import com.common.task.NetCallBack;
import com.common.uitl.StringUtils;
import com.haibei.R;
import com.haibei.activity.consult.ConsultDetailActivity;
import com.haibei.activity.topic.TopicDetailActivity;
import com.haibei.adapter.SearchAdapter;
import com.haibei.adapter.bean.Article;
import com.haibei.adapter.bean.Topic;
import com.haibei.net.NetInterface;

public class SearchActivity extends BaseHaibeiActivity implements View.OnClickListener {

	private EditText editTextInput;
	private ImageView mImageViewClose;
	private BaseTask mBaseTask;
	private ListView listSearches;
	private int INT_TYPE_SEARCH_CONSULT = 1;
	private int INT_TYPE_SEARCH_TOPIC = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	@Override
	public void initView() {
		setContentView(R.layout.activity_search);
		editTextInput = findEditTextById(R.id.editTextInput);
		listSearches = findListViewById(R.id.listSearches);
		findViewById(R.id.buttonCancell).setOnClickListener(this);
		mImageViewClose = findImageViewById(R.id.imageViewClose);
		mImageViewClose.setOnClickListener(this);
		editTextInput.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				String content = editTextInput.getText().toString();
				if (!StringUtils.isEmpty(content)) {
					try {
						if (StringUtils.isEmpty(content)) {
							mImageViewClose.setVisibility(View.INVISIBLE);
						} else {
							mImageViewClose.setVisibility(View.VISIBLE);
						}
						HashMap<String, String> hmap = new HashMap<String, String>();
						// searchText=%E9%98%B2%E8%BE%90%E5%B0%84&offset=0&limit=10
						hmap.put("searchText", URLEncoder.encode(content, "utf-8"));
						hmap.put("offset", "0");
						hmap.put("limit", "10");
						getSearch(INT_TYPE_SEARCH_TOPIC, content).execute(hmap);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});

	}

	private BaseTask getSearch(final int searchType, final String text) {
		if (null != mBaseTask && !mBaseTask.isCancelled()) {
			mBaseTask.cancel(true);
		}
		mBaseTask = new BaseTask(new NetCallBack() {

			@Override
			public void onFinish(NetResult result) {
				if (null != result && result.isSuccess()) {
					if (INT_TYPE_SEARCH_CONSULT == searchType) {
						// 咨询
						ArrayList<Article> articles = (ArrayList<Article>) result.getData()[0];
						buildAdapter(articles, INT_TYPE_SEARCH_CONSULT, text);
					} else {
						// 话题
						ArrayList<Topic> topics = (ArrayList<Topic>) result.getData()[0];
						buildAdapter(topics, INT_TYPE_SEARCH_TOPIC, text);
					}

				}
			}

			@Override
			public NetResult onDoInBack(HashMap<String, String> paramMap) {
				NetResult netResult = null;
				try {
					netResult = NetInterface.searchText(SearchActivity.this, paramMap, searchType);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return netResult;
			}
		});
		return mBaseTask;
	}

	public void showEmptyView(Adapter ada){
		int visibility=((null==ada)|(ada.getCount()==0))?View.VISIBLE:View.GONE;
		findTextViewById(R.id.textViewEmptyView).setVisibility(visibility);
	}
	
	private void buildAdapter(ArrayList list, final int type, String keyword) {
		SearchAdapter sda = null;
		if (INT_TYPE_SEARCH_CONSULT == type) {
			sda = new SearchAdapter(SearchActivity.this, list, keyword, INT_TYPE_SEARCH_CONSULT);
		} else {
			sda = new SearchAdapter(SearchActivity.this, list, keyword, INT_TYPE_SEARCH_TOPIC);
		}
		listSearches.setAdapter(sda);
		showEmptyView(sda);
		listSearches.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (null != listSearches.getAdapter() && null != listSearches.getItemAtPosition(position)) {
					Object o = listSearches.getItemAtPosition(position);
					gotoDetail(o, type);
				}
			}
		});
	}

	private void gotoDetail(Object o, int type) {
		Intent i = new Intent();
		if (INT_TYPE_SEARCH_CONSULT == type) {
			// 去咨询详情
			i.setClass(SearchActivity.this, ConsultDetailActivity.class);
			i.putExtra("article", (Article) o);
		} else {
			i.setClass(SearchActivity.this, TopicDetailActivity.class);
			i.putExtra("topic", (Topic) o);
		}
		startActivity(i);
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.buttonCancell: {
			finish();
		}
			break;
		case R.id.imageViewClose: {
			editTextInput.setText("");
		}
		default:
			break;
		}
	}
}
